// Replication code for 
// Albertus and Denes
// "Private Equity Fund Debt: Agency Costs and Cash Flow Management"

// Clear, set version, and set seed
clear
version 15.1
set seed 42

// Dependencies required for running code
ssc install reghdfe, replace

// Change directory to folder location
cd ""


/////////////////////////////////////////////////////////////////
// Figure 1: Private Equity Structure
// Note: This figure displays the typical structure of PE and 
// 		 the role of SLCs. It does note use any data.
/////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////
// Figure 2: Subscription Lines of Credit
/////////////////////////////////////////////////////////////////
// Construct replication data for Panel A
clear
set obs 60
gen dateq = 182
replace dateq = date[_n-1] + 1 if _n>1
format %tq dateq
label var dateq Quarter
gen slc_total = runiform() * 40 // Match range of data

// Plot Panel A
twoway line slc_total dateq, ///
	yscale(r(0 40)) ylabel(0(10)40) ///
	graphregion(color(white)) graphregion(margin()) bgcolor(white) ///
	ytitle("Aggregate SLC Use ($ billion)")

// Construct replication data for Panel B
clear
set obs 10
gen event_year = 1
replace event_year = event_year[_n-1] + 1 if _n>1
label var event_year Year
gen slc_ratio = runiform() * .25 // Match range of data
label var slc_ratio "Ratio of SLC Use"

// Plot Panel B
twoway line slc_ratio event_year, ///
	xscale(r(1 10)) xlabel(1(1)10) ///
	yscale(r(0 .25)) ylabel(0(.05).25) ///
	lpattern(solid dash dot) ///
	graphregion(color(white)) graphregion(margin()) bgcolor(white)


/////////////////////////////////////////////////////////////////
// Figure 3: Effect of Hypothetical SLC Use on Fund Cash Flows
/////////////////////////////////////////////////////////////////
// Load data
use figure3_data, clear
label var cont_slc "Capital Calls (SLC)"
label var cont_noslc "Capital Calls (No SLC)"

// Plot figure
twoway line cont_slc cont_noslc year, ///
	graphregion(color(white)) graphregion(margin()) bgcolor(white) ///
	legend(order (1 "Capital Calls (SLC)" 2 "Capital Calls (No SLC)") ) ///
	xscale(r(0 10)) xlabel(0(1)10) ///
	yscale(r(-250 50)) ylabel(-250(50)50) ytitle("Millions of dollars") ///
	yline(0, lcolor(black)) ///
	lpattern(solid dash) lcolor(navy navy)


/////////////////////////////////////////////////////////////////
// Figure 4: GP Survey Results
/////////////////////////////////////////////////////////////////
// Load data
use survey_responses, clear
keep if type=="gp"

// Plot Panel A
local labels `"0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%" 50 "50%" 60 "60%""'
graph hbar percent if question=="q2_1", ///
    over(response, sort(order) label(labsize(small))) ///
	ylabel(`labels') ytitle("") ///
	bar(1, color(black)) legend(off) ///
	graphregion(color(white)) graphregion(margin()) bgcolor(white)

// Plot Panel B
local labels `"0 "0%" 5 "5%" 10 "10%" 15 "15%" 20 "20%" 25 "25%""'
graph hbar percent if question=="q2_2", ///
    over(response, sort(order) label(labsize(small))) ///
	ylabel(`labels') ytitle("") ///
	bar(1, color(black)) legend(off) ///
	graphregion(color(white)) graphregion(margin()) bgcolor(white)

// Plot Panel C
local labels `"0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%""'
graph hbar percent if question=="q2_3", ///
    over(response, sort(order) label(labsize(small))) ///
	ylabel(`labels') ytitle("") ///
	bar(1, color(black)) legend(off) ///
	graphregion(color(white)) graphregion(margin()) bgcolor(white)

// Plot Panel D
local labels `"0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%" 50 "50%" 60 "60%""'
graph hbar percent if question=="q2_4", ///
    over(response, sort(order) label(labsize(small))) ///
	ylabel(`labels') ytitle("") ///
	bar(1, color(black)) legend(off) ///
	graphregion(color(white)) graphregion(margin()) bgcolor(white)


/////////////////////////////////////////////////////////////////
// Figure 5: LP Survey Results
/////////////////////////////////////////////////////////////////
// Load data
use survey_responses, clear
keep if type=="lp"

// Plot Panel A
local labels `"0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%" 50 "50%""'
graph hbar percent if question=="q2", ///
    over(response, sort(order) label(labsize(small))) ///
	ylabel(`labels') ytitle("") ///
	bar(1, color(black)) legend(off) ///
	graphregion(color(white)) graphregion(margin()) bgcolor(white)

// Plot Panel B
local labels `"0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%" 50 "50%""'
graph hbar percent if question=="q3_1", ///
    over(response, sort(order) label(labsize(small))) ///
	ylabel(`labels') ytitle("") ///
	bar(1, color(black)) legend(off) ///
	graphregion(color(white)) graphregion(margin()) bgcolor(white)

// Plot Panel C
local labels `"0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%" 50 "50%""'
graph hbar percent if question=="q3_3", ///
    over(response, sort(order) label(labsize(small))) ///
	ylabel(`labels') ytitle("") ///
	bar(1, color(black)) legend(off) ///
	graphregion(color(white)) graphregion(margin()) bgcolor(white)



/////////////////////////////////////////////////////////////////
// Table 1: Effect of Hypothetical SLC Use on Fund Cash Flow
// Note: This table provides cash flows for a hypothetical 
//		 fund using SLCs. It does note use any data.
/////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////
// Table 2: Subscription Lines of Credit
/////////////////////////////////////////////////////////////////
// Construct replication data
clear
set obs 364
gen slc_level = rnormal(47.9, 127)
gen fund_size = rnormal(1280, 2330)
gen leverage = runiform()
gen leverage_uncalled = runiform()
gen slc_length = runiformint(0,60)
gen fund_age_q = runiformint(0,120)
gen young_fund = runiformint(0, 1)

// Provide summary statistics
estpost tabstat *, stat(n mean p50 sd) columns(stat)


/////////////////////////////////////////////////////////////////
// Table 3: Descriptive Statistics
/////////////////////////////////////////////////////////////////
// Panel A: Fund age
// Construct replication data
clear
set obs 768
gen age25 = runiformint(0,120)
gen age50 = runiformint(0,120)
gen age75 = runiformint(0,120)

// Provide summary statistics
estpost tabstat *, stat(n mean p50 sd) columns(stat)


// Panel A: Other variables
// Construct replication data
clear
set obs 26453
gen capital_calls = runiformint(0,10)
gen slc_ind = runiformint(0,1)
gen leverage = runiform()
gen leverage_uncalled = runiform() if slc_ind==1

// Provide summary statistics
estpost tabstat *, stat(n mean p50 sd) columns(stat)


// Panel B
// Construct replication data
clear
set obs 5262
gen irr = runiform()
gen irr_unlev = runiform()
gen tvpi = runiform()
gen tvpi_unlev = runiform()
gen pme = runiform()
gen pme_unlev = runiform()
gen young_fund = runiformint(0, 1)
gen leverage = runiform()
gen leverage_uncalled = runiform() if runiformint(0,1)==1

// Provide summary statistics
estpost tabstat *, stat(n mean p50 sd) columns(stat)


// Panel C: All variables except follow-on
// Construct replication data
clear
set obs 11758
gen slc_ind = runiformint(0,1)
gen leverage = runiform()
gen leverage_uncalled = runiform() if slc_ind==1
gen irr = runiform()
gen irr_unlev = runiform()
gen tvpi = runiform()
gen tvpi_unlev = runiform()
gen pme = runiform()
gen pme_unlev = runiform()

// Provide summary statistics
estpost tabstat *, stat(n mean p50 sd) columns(stat)


// Panel C: Follow-on
// Construct replication data
clear
set obs 527
gen follow_on = runiform()

// Provide summary statistics
estpost tabstat *, stat(n mean p50 sd) columns(stat)


// Panel D: SLC to leverage using uncalled capital
// Construct replication data
clear
set obs 5444
gen slc_ind = runiformint(0,1)
gen leverage = runiform()
gen leverage_uncalled = runiform() if slc_ind==1

// Provide summary statistics
estpost tabstat *, stat(n mean p50 sd) columns(stat)


// Panel D: Delta carry to log fund size
// Construct replication data
clear
set obs 65
gen carry_delta = runiform()
gen carry_delta_ln = ln(carry_delta)
gen slc_level_ln = ln(runiform())
gen fund_size_ln = ln(runiform())

// Provide summary statistics
estpost tabstat *, stat(n mean p50 sd) columns(stat)


/////////////////////////////////////////////////////////////////
// Table 4: Capital Calls
/////////////////////////////////////////////////////////////////
// Panel A
// Construct replication data
clear
set obs 768
gen age25 = runiformint(0,120)
gen age50 = runiformint(0,120)
gen age75 = runiformint(0,120)
gen slc_ind = runiformint(0,1)
gen vintage = runiformint(2005, 2017)

// Run regressions
reg age25 slc_ind i.vintage, vce(robust)
reg age50 slc_ind i.vintage, vce(robust)
reg age75 slc_ind i.vintage, vce(robust)


// Panel B
// Construct replication data
clear
set obs 500
gen fund_id = _n
gen vintage = runiformint(2005, 2017)
gen fund_age_q = runiformint(12, 60)
expand 24
gen capital_calls = runiformint(0,10)
gen slc_ind = runiformint(0, 1)
gen leverage = runiform()
bysort fund_id: replace fund_age_q = fund_age_q[_n-1] + 1 if _n>1

// Run regressions
poisson capital_calls slc_ind i.vintage i.fund_age_q, vce(cluster fund_id)
poisson capital_calls leverage i.vintage i.fund_age_q, vce(cluster fund_id)


/////////////////////////////////////////////////////////////////
// Table 5: Performance
/////////////////////////////////////////////////////////////////
// Construct replication data
clear
set obs 500
gen fund_id = _n
expand 24
gen irr_delta = runiform()
gen tvpi_delta = runiform()
gen pme_delta = runiform()
gen young_fund = runiformint(0, 1)
gen leverage = runiform()

// Run regressions
// Panel A
reg irr_delta, vce(cluster fund_id)
reg irr_delta young_fund, vce(cluster fund_id)
reg irr_delta leverage, vce(cluster fund_id)

// Panel B
reg tvpi_delta, vce(cluster fund_id)
reg tvpi_delta young_fund, vce(cluster fund_id)
reg tvpi_delta leverage, vce(cluster fund_id)

// Panel C
reg pme_delta, vce(cluster fund_id)
reg pme_delta young_fund, vce(cluster fund_id)
reg pme_delta leverage, vce(cluster fund_id)


/////////////////////////////////////////////////////////////////
// Table 6: Fundraising
/////////////////////////////////////////////////////////////////
// Construct replication data
clear
set obs 500
gen fund_id = _n
gen vintage = runiformint(2005, 2017)
gen fund_age_q = runiformint(12, 60)
expand 24
bysort fund_id: replace fund_age_q = fund_age_q[_n-1] + 1 if _n>1
gen slc_ind = runiformint(0, 1)
gen leverage = runiform()
bysort fund_id: gen before = _n<=12 // Defined in Section 5.1; adjusted here for replication purposes

// Run regressions
reghdfe slc_ind before, absorb(vintage fund_age_q) vce(cluster fund_id)
reghdfe leverage before, absorb(vintage fund_age_q) vce(cluster fund_id)


/////////////////////////////////////////////////////////////////
// Table 7: Performance around Fundraising
/////////////////////////////////////////////////////////////////
// Construct replication data
clear
set obs 500
gen fund_id = _n
gen fund_age_q = runiformint(12, 60)
expand 24
bysort fund_id: replace fund_age_q = fund_age_q[_n-1] + 1 if _n>1
gen dif_irr = rnormal(.02, .05)
gen dif_mulitple = rnormal(-.02, .05)
gen dif_pme = rnormal(.03, .05)
bysort fund_id: gen before = _n<=12 // Defined in Section 5.1; adjusted here for replication purposes

// Run regressions
reg dif_irr before, vce(cluster fund_id)
reg dif_mulitple before, vce(cluster fund_id)
reg dif_pme before, vce(cluster fund_id)


/////////////////////////////////////////////////////////////////
// Table 8: LP Compensation
/////////////////////////////////////////////////////////////////
// Construct replication data
clear
set obs 500
gen fund_id = _n
gen public_pension = runiformint(0, 1)
gen vintage = runiformint(2005, 2017)
gen fund_age_q = runiformint(12, 60)
expand 24
bysort fund_id: replace fund_age_q = fund_age_q[_n-1] + 1 if _n>1
gen slc_ind = runiformint(0, 1)
gen leverage = runiform()
bysort fund_id: gen before = _n<=12 // Defined in Section 5.1; adjusted here for replication purposes
gen public_pensionXbefore = public_pension * before

// Run regressions
reghdfe slc_ind public_pensionXbefore public_pension before, absorb(vintage fund_age_q) vce(cluster fund_id)
reghdfe leverage public_pensionXbefore public_pension before, absorb(vintage fund_age_q) vce(cluster fund_id)


/////////////////////////////////////////////////////////////////
// Table 9: Raising a Follow-on Fund
/////////////////////////////////////////////////////////////////
// Construct replication data
clear
set obs 500
gen vintage = runiformint(2005, 2017)
gen follow_on = runiform()
gen slc_ind_before = runiformint(0, 1)
gen leverage_before = runiform()

// Run regressions
reghdfe follow_on slc_ind_before, absorb(vintage) vce(robust)
reghdfe follow_on leverage_before, absorb(vintage) vce(robust)


/////////////////////////////////////////////////////////////////
// Table 10: Reaching the Carried Interest Threshold
/////////////////////////////////////////////////////////////////
// Construct replication data
clear
set obs 250
gen fund_id = _n
gen vintage = runiformint(2005, 2017)
gen fund_age_q = runiformint(12, 60)
expand 24
bysort fund_id: replace fund_age_q = fund_age_q[_n-1] + 1 if _n>1
gen slc_ind = runiformint(0, 1)
gen leverage = runiform()
bysort fund_id: gen before = _n<=12 // Defined in Section 6.1; adjusted here for replication purposes

// Run regressions
reghdfe slc_ind before, absorb(vintage fund_age_q) vce(cluster fund_id)
reghdfe leverage before, absorb(vintage fund_age_q) vce(cluster fund_id)


/////////////////////////////////////////////////////////////////
// Table 11: Carried Interest Compensation
/////////////////////////////////////////////////////////////////
// Construct replication data
clear
set obs 250
gen vintage = runiformint(2005, 2017)
gen log_delta_carry = rnormal(.66, .737)
gen log_slc_level = rnormal(1.6, 1.059)
gen log_fund_size = rnormal(6.2, .854)

// Run regressions
reg log_delta_carry log_slc_level log_fund_size, vce(robust)
reghdfe log_delta_carry log_slc_level log_fund_size, absorb(vintage) vce(robust)


